//
//  CollectionLayout.m
//  Test
//
//  Created by lizq on 16/7/21.
//  Copyright © 2016年 lizq. All rights reserved.
//

#import "CollectionLayout.h"
#import "DecorationView.h"

@implementation CollectionLayout

- (void)prepareLayout {
    [super prepareLayout];
    [self registerClass:[DecorationView class] forDecorationViewOfKind:NSStringFromClass([DecorationView class])];
}

- (CGSize)collectionViewContentSize {
    return self.collectionView.frame.size;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path {
    return [super layoutAttributesForItemAtIndexPath:path];
}

//Decoration View的布局。

- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes* att = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
//    UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexPath];
//    CGFloat height = [self.collectionView numberOfItemsInSection:indexPath.section] / 3 * 50;
    att.frame=CGRectMake(0, indexPath.section * 160, self.collectionView.frame.size.width, 160);
    att.zIndex=-1;
    att.hidden = !(indexPath.section == self.currentSetion);
    return att;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSMutableArray* attributes = [NSMutableArray array];
    //把Decoration View的布局加入可见区域布局。
    for (int y=0; y < [self.collectionView numberOfSections]; y++) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:y];
        [attributes addObject:[self layoutAttributesForDecorationViewOfKind:NSStringFromClass([DecorationView class]) atIndexPath:indexPath]];
    }
    NSArray *tmp = [super layoutAttributesForElementsInRect:rect];
    [attributes addObjectsFromArray:tmp];
    return attributes;
    
}

@end
